#include "register.h"
#include "ui_register.h"
#include <QMessageBox>

Register::Register(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
}

Register::~Register()
{
    delete ui;
}

void Register::setParent(LoginManage *dialog)
{
    if(dialog){
        loginM = dialog;
    }
}


void Register::on_pushButton_ok_clicked()
{
    QString name = ui->lineEdit_name->text();
    QString passwd = ui->lineEdit_passwd->text();
    QString passwdOK = ui->lineEdit_passwd_ok->text();
    bool isAdmin = ui->checkBox_admin->isChecked();
    int level = isAdmin ? 0 : 2;

    if(name.isEmpty()){
        QMessageBox::warning(this,tr("警告"),tr("用户名为空！"));
        return;
    }

    if(passwd.isEmpty()){
        QMessageBox::warning(this,tr("警告"),tr("密码为空！"));
        return;
    }

    if(passwd != passwdOK){
        QMessageBox::warning(this,tr("警告"),tr("两次输入的密码不一致！"));
        return;
    }

    loginM->getUser()->setName(name);
    loginM->getUser()->setPasswd(passwd);
    loginM->getUser()->setLevel(level);
    loginM->getUser()->setIsUpdate(true);
    this->close();
}

void Register::on_pushButton_cancel_clicked()
{
    loginM->getUser()->setIsUpdate(false);
    this->close();
}

void Register::setTiteName(const QString &value)
{
    titeName = value;
    setWindowTitle(titeName);
}

void Register::setUserInfo()
{
    LoginUser *user = loginM->getUser();
    ui->lineEdit_name->setText(user->getName());
    ui->lineEdit_passwd->setText(user->getPasswd());
    ui->lineEdit_passwd_ok->setText(user->getPasswd());

    bool isAdmin = (0 == user->getLevel()) ? true : false;

    ui->checkBox_admin->setChecked(isAdmin);
}
